package xxx.yyy.zzz.movie;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class App {

    private static void downloadMovieIds() {

        final String URL = "https://www.1905.com/mdb/film/list/year-2019/";
        for (int i = 1; i <= 10; i++) {
            StringBuilder baseUrl = new StringBuilder(URL);
            if (i != 1) {
                baseUrl.append("o0d0p").append(i).append(".html");
            }

            Spider.create(new MovieIDProcessor())
                    .addUrl(baseUrl.toString())
                    .thread(1)
                    .addPipeline((resultItems, task) -> {
                        File file = new File("D:/movie/ids.txt");
                        OutputStream os = null;
                        try {
                            os = new BufferedOutputStream(new FileOutputStream(file));
                            IOUtils.writeLines(resultItems.get("ids"), System.lineSeparator(), os);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            IOUtils.closeQuietly(os);
                        }
                    })
                    .run();
        }
    }

    public static void getMoviePlot() throws Exception {
        Reader reader = new BufferedReader(new FileReader(new File("D:/movie/ids.txt")));
        LineIterator it = IOUtils.lineIterator(reader);
        while (it.hasNext()) {
            String movieId = it.nextLine();
            MoviePlotProcessor processor = new MoviePlotProcessor(Integer.valueOf(movieId));
            Spider.create(processor)
                    .addUrl(processor.getUrl())
                    .thread(1)
                    .addPipeline(new ConsolePipeline())
                    .run();
        }
    }

    public static void getMovieInfo() throws Exception {
        Reader reader = new BufferedReader(new FileReader(new File("D:/movie/ids.txt")));
        LineIterator it = IOUtils.lineIterator(reader);
        while (it.hasNext()) {
            String movieId = it.nextLine();
            MovieInfoProcessor processor = new MovieInfoProcessor(movieId);
            Spider.create(processor)
                    .addUrl(processor.getUrl())
                    .thread(1)
                    .addPipeline((resultItems, task) -> {
                        System.out.println("\n");
                        resultItems.getAll().forEach((key, val) -> {
                            System.out.println(key + " : " + val);
                        });
                        System.out.println("\n");
                    })
                    .run();
        }
    }

    public static void main(String[] args) throws Exception {
//        downloadMovieIds();
        getMovieInfo();

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        /*
        System.out.println("\n" + ids.size());

        ExecutorService executorService = Executors.newSingleThreadExecutor();
         */

//        Movie movie = new Movie("2243926");

        Reader reader = new BufferedReader(new FileReader(new File("D:/movie/ids.txt")));
        LineIterator it = IOUtils.lineIterator(reader);
        while (it.hasNext()) {
            String movieId = it.nextLine();
            Movie movie = new Movie(movieId);
            /*
            MovieInfoProcessor infoProcessor = new MovieInfoProcessor(Integer.valueOf(movieId), movie);
            Spider.create(infoProcessor)
                    .addUrl(infoProcessor.getUrl())
                    .thread(executorService, 1)
                    .addPipeline(new ConsolePipeline())
                    .run();


            MoviePlotProcessor performerProcessor = new MoviePlotProcessor(Integer.valueOf(movieId));
            Spider.create(performerProcessor)
                    .addUrl(performerProcessor.getUrl())
//                    .thread(executorService, 1)
                    .addPipeline((resultItems, task) -> {
                        System.out.println("------------------------------");
                        System.out.println("------------------------------");
                        System.out.println("------------------------------");
                        resultItems.getAll().forEach((key, val) -> {
                            System.out.println(key + " : " + val);
                        });
                        System.out.println("------------------------------");
                        System.out.println("------------------------------");
                        System.out.println("------------------------------");
                        System.out.println("------------------------------");
                    })
                    .run();

             */
            /*
            File outputFile = new File("D:/movie/" + movieId + ".txt");
            Writer writer = new BufferedWriter(new FileWriter(outputFile));
            IOUtils.write(movie.toString(), writer);
            IOUtils.closeQuietly(writer);
             */
        }
    }
}

